package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.javaBean.UserProtocolBean;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

public class ServiceProcontrolActivity extends BaseActivity implements ContributorView {
    private TextView tv_headtitle;
    private ImageView regist_back;
    private ContributorPresenter contributorPresenter;
    private Context context;
    private final String MORE_USER_PROTOCOL_FLAG = "MORE_USER_PROTOCOL_FLAG";
    private MyToast myToast;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_procontrol_layout);
        context = this;
        contributorPresenter = new ContributorPresenter(this, context);
        myToast = new MyToast(context);
        initView();
        initData();
    }

    private void initView()
    {
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("用户协议");
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        webView = (WebView) findViewById(R.id.fragment_zxxq_webView);
    }

    private void initData() {
        contributorPresenter.getMoreUserProtocol(MORE_USER_PROTOCOL_FLAG);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(MORE_USER_PROTOCOL_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object message=respObj.getMessage();
                if (code.equals("200") && message!=null) {
                    String messageString=JsonTools.objectToJsonString(message);
                    UserProtocolBean userProtocolBean=JsonTools.jsonStringToObject(messageString,UserProtocolBean.class);

                    String Url = Constant.Head_string + userProtocolBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                } else {
                    myToast.showShort("协议出错");
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }
}
